<?php

namespace app\modules\admin\services;

use app\common\services\BaseService;
use app\config\Constants;
use app\models\GoodsOrder;
use app\models\Merchant;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use yii\helpers\ArrayHelper;

class SettleService extends BaseService{

    public static function download($start_time,$end_time){
        $merchants = Merchant::find()->asArray(true)->all();

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        //默认列宽和行高
        $sheet->getDefaultRowDimension()->setRowHeight(20);
        $sheet->getDefaultColumnDimension()->setWidth(14);
        //设置列宽
        $sheet->getColumnDimension("A")->setWidth(24);
        $sheet->getColumnDimension("B")->setWidth(30);
        //统计表头行高
        $sheet->getRowDimension(1)->setRowHeight(20);
        $sheet->getRowDimension(2)->setRowHeight(20);
        $sheet->getRowDimension(3)->setRowHeight(20);
        $sheet->getRowDimension(5)->setRowHeight(29);
        $sheet->getRowDimension(6)->setRowHeight(20);
        //统计表头垂直居中
        $sheet->getStyleByColumnAndRow(1,1,2,3)
            ->getAlignment()
            ->setVertical(Alignment::VERTICAL_CENTER);
        //统计表头标题
        $sheet->mergeCells("B1:C1");
        $sheet->setCellValue("A1","日期");
        $sheet->setCellValue("A2","交易金额");
        $sheet->setCellValue("A3","交易笔数");
        //表头值
        $sheet->setCellValue("B1",($start_time == $end_time) ? $start_time : $start_time . "-" . $end_time);

        $total_price = 0;
        $total_count = 0;

        //表格头
        $sheet->mergeCells("A5:C5");
        $sheet->setCellValue("A5","交易统计明细");
        $sheet->setCellValue("A6","商户名称");
        $sheet->setCellValue("B6","交易金额");
        $sheet->setCellValue("C6","交易笔数");

        $i = 7;
        foreach($merchants as $v){
            $sheet->getRowDimension($i)->setRowHeight(20);
            $orders = GoodsOrder::find()->asArray(true)
                ->andWhere(['merchant_id'=>$v['merchant_id']])
                ->andWhere(['status'=>Constants::ORDER_COMPLETE])
                ->andWhere(['>=',"date_format(add_time,'%Y-%m-%d')",$start_time])
                ->andWhere(['<=',"date_format(add_time,'%Y-%m-%d')",$end_time])
                ->all();

            $price = !empty($orders) ? array_sum(ArrayHelper::getColumn($orders,"total_price")) : 0;
            $count = !empty($orders) ? count($orders) : 0;
            $total_price += $price;
            $total_count += $count;

            $sheet->setCellValue("A".$i,$v['merchant_name']);
            $sheet->getCell("B".$i)->setValue($price)->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::builtInFormatCode(4));
            $sheet->setCellValue("C".$i,$count);
            $i++;
        }
        //统计数据
        $sheet->getCell("B2")->setValue($total_price)->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::builtInFormatCode(4));
        $sheet->setCellValue("B3",$total_count);

        //表格内容居中
        $sheet->getStyleByColumnAndRow(1,5,3,$i)
            ->getAlignment()
            ->setHorizontal(Alignment::HORIZONTAL_CENTER)
            ->setVertical(Alignment::VERTICAL_CENTER);

        $writer = IOFactory::createWriter($spreadsheet,"Xlsx");
        $writer->save('php://output');
    }
}